
#include "gmatlab.h"

// mxArray * g_irepository_matlab_invoke(GIFunctionInfo *func, mxArray *mxArgs);
void mexFunction(int nlhs, mxArray *lhs[], int nrhs, const mxArray *rhs[])
{
  GIFunctionInfo *f;
  mxArray *ret;

  // Check and convert arguments
  if (nrhs != 2) g_matlab_error("gmatlab:numArg", "g_irepository_matlab_invoke() takes 2 arguments");
  if (nlhs != 1) g_matlab_error("gmatlab:numRet", "g_irepository_matlab_invoke() has 1 return value");
  if (mxGetNumberOfElements(rhs[0]) != 1) g_matlab_error("gmatlab:badArray", "g_irepository_matlab_invoke() Arg 1 must have one element");
  if (mxGetClassID(rhs[0]) != MX_GPOINTER)
    g_matlab_error("gmatlab:badArray", "g_irepository_matlab_invoke() Arg 1 must be int, not %s", mxGetClassName(rhs[0]));
  f = ((gpointer*)mxGetData(rhs[0]))[0];
  
  // Call function
  g_matlab_escape_init();
  lhs[0] = g_irepository_matlab_invoke(f, rhs[1]);
  
  // Prepare return value

  g_matlab_escape_clear();
  g_matlab_cleanup();
}
